package com.yiren.dbaa.module.project_edit.view.fragment;

import android.os.Build;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.yiren.backstage.config.api.LoadConfigApi;
import com.yiren.backstage.config.enums.UnitNameEnum;
import com.yiren.backstage.currency.entity.GradeStandardEntity;
import com.yiren.backstage.currency.entity.JudgeDetailsEntity;
import com.yiren.backstage.currency.entity.JudgeEntity;
import com.yiren.backstage.currency.entity.MonitorCountEntity;
import com.yiren.backstage.currency.entity.ProjectEntity;
import com.yiren.backstage.currency.entity.ProjectJudgeEntity;
import com.yiren.backstage.currency.entity.SiteEntity;
import com.yiren.backstage.currency.enums.DualEnum;
import com.yiren.backstage.currency.enums.ExecuteElementEnum;
import com.yiren.backstage.currency.enums.JudgeChooseTypeEnum;
import com.yiren.backstage.currency.enums.ProjectTypeEnum;
import com.yiren.backstage.currency.enums.StatusEnum;
import com.yiren.backstage.currency.utils.StringUtil;
import com.yiren.backstage.db.dao.bean.SynExamBean;
import com.yiren.backstage.db.dao.cache.GradeStandardCache;
import com.yiren.backstage.db.dao.cache.JudgeCache;
import com.yiren.backstage.db.dao.cache.JudgeDetailsCache;
import com.yiren.backstage.db.dao.cache.MonitorCountCache;
import com.yiren.backstage.db.dao.controller.UserDbController;
import com.yiren.dbaa.R;
import com.yiren.dbaa.currency.api.CommonCallBack;
import com.yiren.dbaa.currency.model.CheckBoxCommonModel;
import com.yiren.dbaa.currency.model.EdittextCommonModel;
import com.yiren.dbaa.currency.model.SpinnerCommonModel;
import com.yiren.dbaa.currency.fragment.LazyLoadFragment;
import com.yiren.dbaa.module.practice_set.site.bean.GradeStandardEditBean;
import com.yiren.dbaa.module.project_edit.bean.JudgeDescribe;
import com.yiren.dbaa.module.project_edit.bean.TextViewPointsBean;
import com.yiren.dbaa.module.project_edit.service.ICreateView;
import com.yiren.dbaa.module.project_edit.service.IDataOperation;
import com.yiren.dbaa.module.project_edit.service.impl.CreateViewImpl;
import com.yiren.dbaa.module.project_edit.service.impl.DataOperationImpl;
import com.yiren.dbaa.module.project_edit.view.dialog.GradeStandardEditDialog;
import com.yiren.dbaa.view.FlowerLayout;
import com.yiren.dbaa.view.toast.MessageBox;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @author lwb
 * @date : 2022/11/9 15:23
 * @description: 规则编辑fragment
 */
public class RuleEditFragment extends LazyLoadFragment {

    @BindView(R.id.ll_start_end_voice)
    LinearLayout llStartEndVoice;
    @BindView(R.id.et_start_voice)
    EditText etStartVoice;
    @BindView(R.id.et_end_voice)
    EditText etEndVoice;
    @BindView(R.id.ll_judge_details)
    LinearLayout llJudgeDetails;

    private SiteEntity site;
    private int child;
    private List<SynExamBean.Parameter> tempParameterList;
    private List<SynExamBean.Parameter> tempMonitorCountsList;
    private boolean isPartner;
    private SynExamBean synExamBean;
    private Gson gs = new Gson();

    /*** 数据库操作 */
    private IDataOperation iDataOperation;
    /*** 当前项目 */
    private ProjectEntity currentProject;
    /*** 创建控件 */
    private ICreateView iCreateView;
    /*** 评判ID的list */
    private List<String> judgeIds;
    /*** 扣分文本list */
    private List<TextViewPointsBean> textViewPointsList;
    /*** 单项选择CheckBox的Map */
    private Map<String, List<CheckBox>> singleCheckBoxMap;

    /*** 下拉框适配器 */
    private ArrayAdapter<String> spinnerAdapter;

    private List<CheckBoxCommonModel> checkBoxCommonModels;
    private List<EdittextCommonModel> edittextCommonModels;
    private List<SpinnerCommonModel> spinnerCommonModels;
    private List<EdittextCommonModel> edittextCountModels;

    @Override
    protected int setContentView() {
        return R.layout.fragment_rule_edit;
    }

    @Override
    protected void init() {
        initData();
        setSpinnerAdapter();
        loadJudgeRule(child);
        refreshProjectList();
        setViewListener();
    }

    private void initData() {
        site = (SiteEntity) getArguments().getSerializable("site");
        child = getArguments().getInt("child");
        tempParameterList = new ArrayList<>();
        tempMonitorCountsList = new ArrayList<>();
        isPartner = LoadConfigApi.loadUserParamConfig(getActivity()).isPartner();
        synExamBean = gs.fromJson(isPartner ? site.getPartneModifyVal() : site.getCoachModifyVal(), new TypeToken<SynExamBean>() {
        }.getType());
        if (null == synExamBean) synExamBean = new SynExamBean();
        iDataOperation = new DataOperationImpl();
        iCreateView = new CreateViewImpl();
        judgeIds = new ArrayList<String>();
        textViewPointsList = new ArrayList<>();
        singleCheckBoxMap = new HashMap<>();

        checkBoxCommonModels = new ArrayList<>();
        edittextCommonModels = new ArrayList<>();
        spinnerCommonModels = new ArrayList<>();
        edittextCountModels = new ArrayList<>();
    }

    private void setSpinnerAdapter() {
        //下拉框适配器和样式
        UnitNameEnum[] unitNames = UnitNameEnum.values();
        String[] spinnerList = new String[unitNames.length];
        for (int i = 0; i < unitNames.length; i++) {
            spinnerList[i] = unitNames[i].getDescription();
        }
        spinnerAdapter = new ArrayAdapter<String>(getActivity(), R.layout.item_spinner_array, spinnerList);
        spinnerAdapter.setDropDownViewResource(R.layout.item_spinner_dropdown);
    }

    public void loadJudgeRule(int position) {
        //获取所有项目列表
        List<ProjectEntity> projectList = iDataOperation.getProjectList();
        //项目评判列表
        List<ProjectJudgeEntity> projectJudgeList = iDataOperation.getProjectJudgeList();
        //获取当前的项目
        currentProject = projectList.get(position);
        //全程评判不显示语音
        if (currentProject.getName().equals(ProjectTypeEnum.FULL_JUDGE.getName()) || currentProject.getName().equals("综合评判")) {
            llStartEndVoice.setVisibility(View.GONE);
        } else {
            llStartEndVoice.setVisibility(View.VISIBLE);
            //开始语音
            if (!StringUtil.isEmpty(currentProject.getStartVoiceName())) {
                etStartVoice.setText(currentProject.getStartVoiceName());
            } else {
                etStartVoice.setText(currentProject.getStartContent());
            }
            //结束语音
            if (!StringUtil.isEmpty(currentProject.getEndVoiceName())) {
                etEndVoice.setText(currentProject.getEndVoiceName());
            } else {
                etEndVoice.setText(currentProject.getEndContent());
            }
        }
        llJudgeDetails.removeAllViews();
        for (ProjectJudgeEntity projectJudge : projectJudgeList) {
            if (projectJudge.getProjectId().equals(currentProject.getId())) {
                JudgeEntity judge = JudgeCache.getJudge(projectJudge.getJudgeId());
                if (judge.getStatus().equals(StatusEnum.EDITABLE.toString())) {
                    judgeIds.add(projectJudge.getJudgeId());
                }
            }
        }
    }

    private void refreshProjectList() {
        try {
            for (int i = 0; i < judgeIds.size(); i++) {
                JudgeEntity judge = JudgeCache.getJudge(judgeIds.get(i));
                List<JudgeDescribe> judgeDescribeList = gs.fromJson(judge.getDescribe(), new TypeToken<List<JudgeDescribe>>() {
                }.getType());
                if (judgeDescribeList == null)
                    return;
                boolean isAddViewPoints = false; //是否查看扣分
                LinearLayout linearLayout = iCreateView.addChildLinearLayout(getActivity());
                FlowerLayout flowerLayout;
                if (judge.getParentId() == null || judge.getParentId().equals("0")) {
                    isAddViewPoints = true;
                    flowerLayout = iCreateView.addParentFlowerLayout(getActivity());
                    CheckBox checkBox = iCreateView.addCheckBox(getActivity());
                    checkBox.setId(i);
                    if (judge.getIsEnable().contains(DualEnum.YES.toString())) {
                        checkBox.setChecked(true);
                    } else {
                        checkBox.setChecked(false);
                    }
                    linearLayout.addView(checkBox);
                    checkBoxCommonModels.add(new CheckBoxCommonModel(checkBox, checkBox.isChecked(), judge.getId()));
                } else {
                    flowerLayout = iCreateView.addChildFlowerLayout(getActivity());
                    CheckBox checkBox = iCreateView.addChildCheckBox(getActivity());
                    checkBox.setId(i);
                    if (judge.getIsEnable().contains(DualEnum.YES.toString())) {
                        checkBox.setChecked(true);
                    } else {
                        checkBox.setChecked(false);
                    }
                    linearLayout.addView(checkBox);
                    checkBoxCommonModels.add(new CheckBoxCommonModel(checkBox, checkBox.isChecked(), judge.getId()));

                    if (JudgeCache.getJudge(judge.getParentId()).getChooseType().equals(JudgeChooseTypeEnum.SINGLE.toString())) {
                        //如果父类为单选
                        List<CheckBox> childs;
                        if (singleCheckBoxMap.containsKey(judge.getParentId())) {
                            childs = singleCheckBoxMap.get(judge.getParentId());
                        } else {
                            childs = new ArrayList<>();
                        }
                        childs.add(checkBox);
                        singleCheckBoxMap.put(judge.getParentId(), childs);
                    }
                }
                for (JudgeDescribe judgeDescribe : judgeDescribeList) {
                    switch (judgeDescribe.getKey()) {
                        case 1:  //描述文本
                            TextView tvDescribe = iCreateView.addTextView(getActivity());
                            tvDescribe.setGravity(Gravity.CENTER_VERTICAL);
                            tvDescribe.setText(judgeDescribe.getValue());
                            flowerLayout.addView(tvDescribe);
                            break;
                        case 2://条件值和条件要素
                            JudgeDetailsEntity judgeCondition = JudgeDetailsCache.getJudgeDetails(judgeDescribe.getValue());
                            EditText etCondition = iCreateView.addEditText(getActivity());
                            etCondition.setText(judgeCondition.getConditionValue() + "");
                            flowerLayout.addView(etCondition);
                            edittextCommonModels.add(new EdittextCommonModel(etCondition, judgeCondition.getConditionValue() + "", judgeCondition.getId(), 2));
                            Spinner spinner = iCreateView.addSpinner(getActivity());
                            //加载适配器
                            spinner.setAdapter(spinnerAdapter);
                            if (judgeCondition.getConditionElement().contains(ExecuteElementEnum.TIME.toString())) {
                                spinner.setSelection(0);
                            } else if (judgeCondition.getConditionElement().contains(ExecuteElementEnum.DISTANCE.toString())) {
                                spinner.setSelection(1);
                            }
                            flowerLayout.addView(spinner);
                            spinnerCommonModels.add(new SpinnerCommonModel(spinner, spinner.getSelectedItemPosition(), judgeCondition.getId(), 3));
                            break;
                        case 3://监控运算值
                            MonitorCountEntity monitorCount = MonitorCountCache.getMonitorCount(judgeDescribe.getValue());
                            if (monitorCount.getRelationalOperator().equals("TO")) {
                                EditText etCount1 = iCreateView.addEditText(getActivity());
                                etCount1.setText(monitorCount.getMinValue() + "");
                                flowerLayout.addView(etCount1);
                                TextView tvDescribe2 = iCreateView.addTextView(getActivity());
                                tvDescribe2.setText("至");
                                flowerLayout.addView(tvDescribe2);
                                EditText etCount2 = iCreateView.addEditText(getActivity());
                                etCount2.setText(monitorCount.getMaxValue() + "");
                                flowerLayout.addView(etCount2);
                            } else {
                                EditText etCount3 = iCreateView.addEditText(getActivity());
                                etCount3.setText(monitorCount.getValue() + "");
                                flowerLayout.addView(etCount3);
                                edittextCountModels.add(new EdittextCommonModel(etCount3, monitorCount.getValue(), monitorCount.getId(), 1));
                            }
                            break;
                        case 4://执行值和执行要素
                            JudgeDetailsEntity judgeExecute = JudgeDetailsCache.getJudgeDetails(judgeDescribe.getValue());
                            EditText etExecute = iCreateView.addEditText(getActivity());
                            etExecute.setText(judgeExecute.getExecuteValue() + "");
                            flowerLayout.addView(etExecute);
                            edittextCommonModels.add(new EdittextCommonModel(etExecute, judgeExecute.getExecuteValue() + "", judgeExecute.getId(), 4));
                            Spinner spinner2 = iCreateView.addSpinner(getActivity());
                            //加载适配器
                            spinner2.setAdapter(spinnerAdapter);
                            if (judgeExecute.getExecuteElement().contains(ExecuteElementEnum.TIME.toString())) {
                                spinner2.setSelection(0);
                            } else if (judgeExecute.getExecuteElement().contains(ExecuteElementEnum.DISTANCE.toString())) {
                                spinner2.setSelection(1);
                            }
                            flowerLayout.addView(spinner2);
                            spinnerCommonModels.add(new SpinnerCommonModel(spinner2, spinner2.getSelectedItemPosition(), judgeExecute.getId(), 5));
                            break;
                    }
                }
                linearLayout.addView(flowerLayout);
                if (isAddViewPoints) {
                    GradeStandardEntity gradeStandard = JudgeDetailsCache.getGradeStandardByJudgeId(judge.getId());
                    TextView textView = iCreateView.addTextView(getActivity());
                    textView.setText("查看扣分");
                    textView.setTextColor(getActivity().getResources().getColor(R.color.blue_select_text));
                    linearLayout.addView(textView);
                    textViewPointsList.add(new TextViewPointsBean(judge.getId(), textView, gradeStandard));
                }
                llJudgeDetails.addView(linearLayout);
                llJudgeDetails.addView(iCreateView.addDividerLine(getActivity()));
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            singleCheckboxMonitor();
            setPointsListener();
        }
    }

    /**
     * 单选监听
     */
    private void singleCheckboxMonitor() {
        for (String key : singleCheckBoxMap.keySet()) {
            List<CheckBox> checkBoxList = singleCheckBoxMap.get(key);
            for (CheckBox checkBox : checkBoxList) {
                checkBox.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        for (CheckBox checkBox : checkBoxList) {
                            if (v.getId() == checkBox.getId()) {
                                checkBox.setChecked(true);
                            } else {
                                checkBox.setChecked(false);
                            }
                        }
                    }
                });
            }
        }
    }

    /**
     * 设置扣分文本监听
     */
    private void setPointsListener() {
        for (TextViewPointsBean textViewPoint : textViewPointsList) {
            GradeStandardEntity gradeStandard = textViewPoint.getGradeStandard();
            textViewPoint.getTextView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != gradeStandard && gradeStandard.getId() != null) {
                        GradeStandardEditDialog.show(getActivity(), gradeStandard.getGrade(), gradeStandard.getStandardContent(),
                                gradeStandard.getAbbreviationContent(), new CommonCallBack() {
                                    @Override
                                    public void success(Object result) {
                                        GradeStandardEditBean grade = (GradeStandardEditBean) result;
                                        if (!StringUtils.equals(grade.getContent(), gradeStandard.getStandardContent()) || !StringUtils.equals(grade.getContentjx(), gradeStandard.getAbbreviationContent())
                                                || gradeStandard.getGrade() != grade.getSelectPoints()) {
                                            if (synExamBean.getGradeStandardMap() == null)
                                                synExamBean.setGradeStandardMap(new HashMap<>());
                                            gradeStandard.setStandardContent(grade.getContent());
                                            gradeStandard.setAbbreviationContent(grade.getContentjx());
                                            gradeStandard.setGrade(grade.getSelectPoints());
                                            synExamBean.getGradeStandardMap().put(gradeStandard.getId(), new SynExamBean.ObjectClass<>(gradeStandard, 2));
                                            GradeStandardCache.getAllGradeStandard().put(gradeStandard.getId().hashCode(), gradeStandard);
                                            if (isPartner) {
                                                site.setPartneModifyVal(gs.toJson(synExamBean));
                                            } else {
                                                site.setCoachModifyVal(gs.toJson(synExamBean));
                                            }
                                            UserDbController.getInstance(false).update(site);
                                        }
                                    }
                                });
                    } else {
                        MessageBox.show(getActivity(), "暂无扣分详情!");
                    }

                }
            });
        }
    }

    /**
     * 设置View的监听事件
     */
    private void setViewListener() {
        setCheckBox();
        setEdittext();
        setSpinner();
        setCountEdittext();
    }

    private void setCheckBox() {
        for (CheckBoxCommonModel model : checkBoxCommonModels) {
            model.getCb().setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked != model.isInitValue()) {
                        boolean hasKey = false;  //集合中是否有这个元素
                        for (int i = 0; i < tempParameterList.size(); i++) {
                            if (tempParameterList.get(i).getId().equals(model.getKey()) && tempParameterList.get(i).getType() == 1) {
                                hasKey = true;
                                tempParameterList.get(i).setValue(isChecked ? DualEnum.YES.toString() : DualEnum.NO.toString());
                                break;
                            }
                        }
                        if (!hasKey)
                            tempParameterList.add(new SynExamBean.Parameter(1, model.getKey(), isChecked ? DualEnum.YES.toString() : DualEnum.NO.toString()));
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            tempParameterList.removeIf(parameter -> parameter.getId().equals(model.getKey()) && parameter.getType() == 1);
                        } else {
                            for (int i = 0; i < tempParameterList.size(); i++) {
                                if (tempParameterList.get(i).getId().equals(model.getKey()) && tempParameterList.get(i).getType() == 1) {
                                    tempParameterList.remove(i);
                                    break;
                                }
                            }
                        }
                    }
                }
            });
        }
    }

    private void setEdittext() {
        for (EdittextCommonModel model : edittextCommonModels) {
            model.getEt().addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!s.toString().equals(model.getInitValue())) {
                        boolean hasKey = false;  //集合中是否有这个元素
                        for (int i = 0; i < tempParameterList.size(); i++) {
                            if (tempParameterList.get(i).getId().equals(model.getKey()) && tempParameterList.get(i).getType() == model.getType()) {
                                hasKey = true;
                                tempParameterList.get(i).setValue(s.toString());
                                break;
                            }
                        }
                        if (!hasKey)
                            tempParameterList.add(new SynExamBean.Parameter(model.getType(), model.getKey(), s.toString()));
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            tempParameterList.removeIf(parameter -> parameter.getId().equals(model.getKey()) && parameter.getType() == model.getType());
                        } else {
                            for (int i = 0; i < tempParameterList.size(); i++) {
                                if (tempParameterList.get(i).getId().equals(model.getKey()) && tempParameterList.get(i).getType() == model.getType()) {
                                    tempParameterList.remove(i);
                                    break;
                                }
                            }
                        }
                    }
                }
            });
        }
    }

    private void setSpinner() {
        for (SpinnerCommonModel model : spinnerCommonModels) {
            model.getSp().setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                @Override
                public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                    if (position != model.getInitValue()) {
                        boolean hasKey = false;  //集合中是否有这个元素
                        for (int i = 0; i < tempParameterList.size(); i++) {
                            if (tempParameterList.get(i).getId().equals(model.getKey()) && tempParameterList.get(i).getType() == model.getType()) {
                                hasKey = true;
                                tempParameterList.get(i).setValue(position == 0 ? ExecuteElementEnum.TIME.toString() : ExecuteElementEnum.DISTANCE.toString());
                                break;
                            }
                        }
                        if (!hasKey)
                            tempParameterList.add(new SynExamBean.Parameter(model.getType(), model.getKey(), position == 0 ? ExecuteElementEnum.TIME.toString() : ExecuteElementEnum.DISTANCE.toString()));
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            tempParameterList.removeIf(parameter -> parameter.getId().equals(model.getKey()) && parameter.getType() == model.getType());
                        } else {
                            for (int i = 0; i < tempParameterList.size(); i++) {
                                if (tempParameterList.get(i).getId().equals(model.getKey()) && tempParameterList.get(i).getType() == model.getType()) {
                                    tempParameterList.remove(i);
                                    break;
                                }
                            }
                        }
                    }
                }

                @Override
                public void onNothingSelected(AdapterView<?> parent) {

                }
            });
        }
    }

    private void setCountEdittext() {
        for (EdittextCommonModel model : edittextCountModels) {
            model.getEt().addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (!s.toString().equals(model.getInitValue())) {
                        boolean hasKey = false;  //集合中是否有这个元素
                        for (int i = 0; i < tempMonitorCountsList.size(); i++) {
                            if (tempMonitorCountsList.get(i).getId().equals(model.getKey()) && tempMonitorCountsList.get(i).getType() == model.getType()) {
                                hasKey = true;
                                tempMonitorCountsList.get(i).setValue(s.toString());
                                break;
                            }
                        }
                        if (!hasKey)
                            tempMonitorCountsList.add(new SynExamBean.Parameter(model.getType(), model.getKey(), s.toString()));
                    } else {
                        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                            tempMonitorCountsList.removeIf(parameter -> parameter.getId().equals(model.getKey()) && parameter.getType() == model.getType());
                        } else {
                            for (int i = 0; i < tempMonitorCountsList.size(); i++) {
                                if (tempMonitorCountsList.get(i).getId().equals(model.getKey()) && tempMonitorCountsList.get(i).getType() == model.getType()) {
                                    tempMonitorCountsList.remove(i);
                                    break;
                                }
                            }
                        }
                    }
                }
            });
        }
    }

    @OnClick(R.id.tv_save_param)
    public void onViewClicked() {
        saveStartOrEndVoice();
        if (tempParameterList.size() != 0 || tempMonitorCountsList.size() != 0) {
            if (tempParameterList.size() > 0) {
                List<SynExamBean.Parameter> finalParameters = new ArrayList<>();
                List<SynExamBean.Parameter> parameters = synExamBean.getParameters();
                if (parameters.size() != 0) {
                    List<Integer> list = new ArrayList<>();  //记录重复的索引值
                    a:
                    for (int i = 0; i < parameters.size(); i++) {
                        b:
                        for (int j = 0; j < tempParameterList.size(); j++) {
                            if (parameters.get(i).getId().equals(tempParameterList.get(j).getId()) && parameters.get(i).getType() == tempParameterList.get(j).getType()) {
                                list.add(i);
                                break b;
                            }
                        }
                    }
                    for (int i = 0; i < parameters.size(); i++) {
                        if (!list.contains(i)) {
                            finalParameters.add(parameters.get(i));
                        }
                    }
                }
                finalParameters.addAll(tempParameterList);
                synExamBean.setParameters(finalParameters);
            }

            if (tempMonitorCountsList.size() > 0) {
                List<SynExamBean.Parameter> finalParameters = new ArrayList<>();
                List<SynExamBean.Parameter> parameters = synExamBean.getMonitorCounts();
                if (parameters.size() != 0) {
                    List<Integer> list = new ArrayList<>();  //记录重复的索引值
                    a:
                    for (int i = 0; i < parameters.size(); i++) {
                        b:
                        for (int j = 0; j < tempMonitorCountsList.size(); j++) {
                            if (parameters.get(i).getId().equals(tempMonitorCountsList.get(j).getId()) && parameters.get(i).getType() == tempMonitorCountsList.get(j).getType()) {
                                list.add(i);
                                break b;
                            }
                        }
                    }
                    for (int i = 0; i < parameters.size(); i++) {
                        if (!list.contains(i)) {
                            finalParameters.add(parameters.get(i));
                        }
                    }
                }
                finalParameters.addAll(tempMonitorCountsList);
                synExamBean.setMonitorCounts(finalParameters);
            }
            if (isPartner) {
                site.setPartneModifyVal(gs.toJson(synExamBean));
            } else {
                site.setCoachModifyVal(gs.toJson(synExamBean));
            }
            UserDbController.getInstance(false).update(site);
        }
        MessageBox.show(getActivity(), "保存成功!");
    }

    /**
     * 保存开始或结束语音
     */
    private void saveStartOrEndVoice() {
        if (llStartEndVoice.getVisibility() == View.VISIBLE) {
            currentProject.setStartContent(etStartVoice.getText().toString().trim());
            currentProject.setEndContent(etEndVoice.getText().toString().trim());
            iDataOperation.updateProject(currentProject);
        }
    }
}
